<?php

session_start();
require 'connection.php';

$app = new \Slim\Slim();
$app->get('/art_rep/cat/:cat_id', authorize('admin'), 'cat');
$app->get('/art_rep/valued/', authorize('admin'), 'valued');
$app->get('/art_rep/stock/', authorize('admin'), 'stock');
$app->run();

function cat($cat_id) {
    $sql_list = "SELECT id, code, name, model, category_id FROM articles WHERE category_id = :category_id";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql_list);
        $stmt->bindParam("category_id", $cat_id);
        $stmt->execute();
        $rst_find = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($rst_find);
    } catch (PDOException $e) {
        echo '{"error":{"text":' . $e->getMessage() . '}}';
        return;
    }
}

function valued() {
    $sql_list = "SELECT * FROM articles";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql_list);
        $stmt->bindParam("category_id", $cat_id);
        $stmt->execute();
        $rst_find = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($rst_find);
    } catch (PDOException $e) {
        echo '{"error":{"text":' . $e->getMessage() . '}}';
        return;
    }
}

function stock() {
    $sql_list = "SELECT * FROM articles";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql_list);
        $stmt->bindParam("category_id", $cat_id);
        $stmt->execute();
        $rst_find = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($rst_find);
    } catch (PDOException $e) {
        echo '{"error":{"text":' . $e->getMessage() . '}}';
        return;
    }
}


?>